# -*- coding: utf-8 -*-
#coding=utf-8

import config
from action.user.base import userAction
from model.user import user
from model.favorite import favorite
from model.product import product

'''
User controller: producing favorite user views.
'''
class refererAction(userAction):
    def __init__(self, name = '收藏管理'):
        userAction.__init__(self, name)

    def GET(self, name):
        if name == 'list':
            return self.list()
        elif name == 'delete':
            return self.delete()
        elif name == 'edit':
            return self.edit()
        else:
            return self.notFound()

    def POST(self, name):
        if name == 'modify':
            return self.modify()
        elif name == 'deleteBatch':
            return self.deleteBatch()
        else:
            return self.notFound()

    def list(self):
        inputParams = self.getInput()
        page = int(inputParams['page']) if inputParams.has_key('page') else 1
        count = config.COUNT_PER_PAGE
        offset= (page-1)*count if page > 0 else 0
   
        condition = {'user':self.userid}
        favoriteObj = favorite()
        listData = favoriteObj.getOne('COUNT(*) AS `total`', condition)
        totalCount = listData['total']
        favoriteList = favoriteObj.getList('*', condition, '', str(offset) + ',' + str(count))
        
        pageString = self.getPageStr(self.makeUrl('/user/favorite/list'), page, count, totalCount)
        self.privData['FAVORITE_LIST'] = favoriteList
        self.privData['PAGE_STRING'] = pageString

        return self.display('favoriteList')

    def delete(self):
        inputParams = self.getInput()

        try:
            condition = { 'id': int(inputParams['id']) }
            favorite().delete(condition)
        except Exception, e:
            return self.error(msg = '收藏删除失败: %s' % e, url=self.makeUrl('/user/favorite/list'))
    
        return self.success(msg='收藏删除成功', url=self.makeUrl('/user/favorite/list'))

    def deleteBatch(self):
        inputParams = self.getInput()
        try:
            condition = ' id IN (' + inputParams['delitems'] +')'
            favorite().delete(condition)
        except Exception, e:
            return self.error(msg='收藏删除失败: %s' % e, url=self.makeUrl('/user/favorite/list'))

        return self.success(msg='收藏删除成功', url=self.makeUrl('/user/favorite/list'))

    def modify(self):
        inputParams= self.getInput()

        try:
            data={
                'product':        int(inputParams['product']),
            }
            favorite().update(data, {'id': int(inputParams['id'])})
        except Exception, e:
            return self.error(msg = '收藏修改失败: %s' % e, url=self.makeUrl('/user/favorite/list',{'id':inputParams['product']}))

        return self.success('收藏修改成功', url=self.makeUrl('/user/favorite/list',{'id':inputParams['product']}))

    def edit(self):
        inputParams = self.getInput()

        favoriteObj = favorite().getOne('*', {'id': int(inputParams['id'])})
        if not favoriteObj:
            return self.error(msg = '收藏不存在', url=self.makeUrl('/user/favorite/list',{'id':''}))

        self.privData['FAVORITE'] =   favoriteObj

        condition = {'owner':self.userid}
        productList = product().getList('*', condition)
        self.privData['PRODUCT_LIST'] = {}
        for item in productList:
            self.privData['PRODUCT_LIST'][item['id']] = item['name']

        return self.display('favoriteEdit')
